package com.chenfan.common.extension.dict;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.ArrayUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;

/**
 * 字典流
 *
 * @author wulg
 * @date 2021-09-11
 **/
@FunctionalInterface
public interface DictStream<T> {

    /**
     * of
     * @param targets
     * @param <T>
     * @return
     */
    static <T> List<Target<T>> of(Target<T>... targets) {
        if(ArrayUtils.isEmpty(targets)) {
            return null;
        }
        List<Target<T>> targetList = new ArrayList<>(targets.length);
        for(Target<T> target : targets) {
            targetList.add(target);
        }
        return targetList;
    }

    /**
     * map
     * @param dictType
     * @param dictNameFunc
     * @param dictValueFunc
     * @param <T>
     * @return
     */
    static <T> Target<T> map(String dictType, Function<T, ? extends Serializable> dictNameFunc, BiConsumer<T, String> dictValueFunc) {
        return new Target<T>(dictType, dictNameFunc, dictValueFunc);
    }

    /**
     * 集合
     * @return
     */
    List<Target<T>> collect();

    @Getter
    @AllArgsConstructor
    class Target<T> {
        private String dictType;
        private Function<T, ? extends Serializable> function;
        private BiConsumer<T, String> biConsumer;
    }
}
